import Vue from 'vue'
import Vuex from 'vuex'
import app from './modules/app'
import user from './modules/user'
import permission from './modules/permission'
import getters from './getters'
import {
	logout,
	upLoginCache

} from '@/api/index'

Vue.use(Vuex)
export default new Vuex.Store({
	modules: {
		app,
		user,
		permission
	},
	state: {
		cachedViews: [],
		visitedViews: [],
		activePage: null,
		deptTree: [], //部门树
		allDeptTree: [],
		allFarmDeptTree: [], //带农场的部门树
		schoolTree: [],
		crumbList: [],
		//全局机构信息
		globalOrg: localStorage.getItem('org') && localStorage.getItem('org').startsWith('{') ? JSON.parse(
			localStorage.getItem('org')) : {},
		//倒计时
		countDown: localStorage.getItem('countDown') || 0,
		isCountDown: localStorage.getItem('isCountDown') || 0, //是否启用倒计时
		//套题数据
		testData: localStorage.getItem('testData') || {},
		answerTime: null, //发题时间
		classId: localStorage.getItem('classId') || '',
		//临时题数据
		tempTestData: {},
		hostname: location.hostname,
		isPro: location.hostname == 'zdx.ai-boats.com',
		rectlist: [],
		isNewTemplate: localStorage.getItem('isNewTemplate') || '1'
	},
	mutations: {
		ADD_VISITED_VIEW: (state, view) => {
			if (state.visitedViews.some(v => v.path === view.path)) return
			state.visitedViews.push(
				Object.assign({}, view, {
					title: view.meta.title || 'no-name'
				})
			)

		},
		ADD_CACHED_VIEW: (state, view) => {
			if (state.cachedViews.includes(view.name)) return
			console.log('view.meta.keepAlive', view.meta.keepAlive)
			if (!!view.meta.keepAlive) {
				state.cachedViews.push(view.name)
			}
		},
		DEL_VISITED_VIEW: (state, view) => {
			for (const [i, v] of state.visitedViews.entries()) {
				if (v.path === view.path) {
					state.visitedViews.splice(i, 1)
					break
				}
			}
		},
		DEL_CACHED_VIEW: (state, view) => {
			const index = state.cachedViews.indexOf(view.name)
			index > -1 && state.cachedViews.splice(index, 1)
		},
		SET_VISITED_VIEW: (state, view) => {
			if (view && view.length > 0) {
				state.cachedViews = view.map(item => item.name)
			} else {
				state.cachedViews = []
			}
		},
		SET_CACHED_VIEW: (state, view) => {
			if (view && view.length > 0) {
				state.visitedViews = [...view]
			} else {
				state.cachedViews = []
			}
		},
		SET_ACTIVE_PAGE: (state, view) => {
			state.activePage = view && view.matched && view.matched.length > 0 ? view.matched[0].name : view
				.name;
		},
		SET_DEPTTREE: (state, deptTree) => {
			state.deptTree = [...deptTree];
		},
		SET_ALLDEPTTREE: (state, deptTree) => {
			state.allDeptTree = [...deptTree];
		},
		SET_ALLFARMDEPTTREE: (state, deptTree) => {
			state.allFarmDeptTree = [...deptTree];
		},
		SET_CRUMBLIST: (state, crumbList) => {
			state.crumbList = crumbList;
		},
		SET_GLOBALORG: (state, org) => {
			localStorage.setItem('org', JSON.stringify(org));
			state.globalOrg = {
				...org
			};
			//upLoginCache(state.globalOrg.orgId).then(da => {}).finally(() => {});
		},
		//倒计时
		SET_COUNTDOWN(state, countDown) {
			state.countDown = countDown;
			localStorage.setItem('countDown', countDown);
		},
		//是否开始倒计时
		SET_IS_COUNTDOWN(state, isCountDown) {
			state.isCountDown = isCountDown;
			localStorage.setItem('isCountDown', isCountDown);
		},
		//发题时间
		SET_AnswerTime(state, answerTime) {
			state.answerTime = answerTime;
			localStorage.setItem('answerTime', answerTime);
		},
		SET_ClASS_ID(state, classId) {
			state.classId = classId;
			localStorage.setItem('classId', classId);
		},
		//存储临时题
		SET_tempTestData(state, tempTestData) {
			state.tempTestData = tempTestData;
			// localStorage.setItem('tempTestData', tempTestData);
			Vue.ls.set('tempTestData', tempTestData)
		},
		//清除临时题
		REMOVE_tempTestData(state, tempTestData) {
			state.tempTestData = {};
			Vue.ls.remove('tempTestData');
		},
		//存储套题数据
		SET_testData(state, testData) {
			state.testData = testData;
			localStorage.setItem('testData', testData);
		},
		//存储已经答一半的主观题，或者客观题的答题记录，防止跳转统计页面查询统计结果太慢
		SET_HASANSWERRECTLIST(state, rectlist) {
			state.rectlist = rectlist;
		},
		SET_isNewTemplate: (state, isNewTemplate) => {
			state.isNewTemplate = isNewTemplate;
			localStorage.setItem('isNewTemplate', isNewTemplate);
		},
	},
	actions: {
		addView({
			dispatch,
		}, view) {
			dispatch('addVisitedView', view)
			dispatch('addCachedView', view)

		},
		delView({
			dispatch,
			state
		}, view) {
			return new Promise(resolve => {
				dispatch('delVisitedView', view)
				dispatch('delCachedView', view)
				resolve({
					visitedViews: [...state.visitedViews],
					cachedViews: [...state.cachedViews]
				})
			})
		},
		addVisitedView({
			commit,
			state
		}, view) {
			commit('SET_ACTIVE_PAGE', view)
			commit('ADD_VISITED_VIEW', view)
		},
		addCachedView({
			commit,
			state
		}, view) {
			commit('ADD_CACHED_VIEW', view)
		},
		delVisitedView({
			commit,
			state
		}, view) {
			return new Promise(resolve => {
				commit('DEL_CACHED_VIEW', view)
				resolve([...state.cachedViews])
			})
		},
		delCachedView({
			commit,
			state
		}, view) {
			return new Promise(resolve => {
				commit('DEL_CACHED_VIEW', view)
				resolve([...state.cachedViews])
			})
		},
		setView({
			commit,
			state
		}, view) {
			commit('SET_VISITED_VIEW', view)
			commit('SET_CACHED_VIEW', view)
		},
		setCachedView({
			commit,
			state
		}, view) {
			commit('SET_CACHED_VIEW', view)
		},
		Logout({
			commit,
			state
		}) {
			// logout
			return new Promise((resolve) => {
				logout().then(da => {}).finally(() => {
					//Vue.ls.clear();
					const localkeys = Object.keys(window.localStorage);

					// console.log("数据呀呀呀呀"+JSON.stringify(localkeys))
					// for(var i=0;i<localkeys.length;i++){
					// 	if(!['iot__tempTestData','iot__testData','iot__testComponet','iot__beginClass','iot__tempTopicData'].includes(localkeys[i])){
					// 		Vue.ls.remove(localkeys[i])
					// 	}
					// }

					for (var i = 0; i < localkeys.length; i++) {
						if (!['iot__tempTopicData'].includes(localkeys[i])) {
							Vue.ls.remove(localkeys[i])
						}
					}
					sessionStorage.removeItem('access_token');
					sessionStorage.removeItem('refresh_token');
					commit('SET_CACHED_VIEW');
					commit('SET_menuState', false);
					resolve()
				});
			})
		},


	},
	getters
})